// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   ESSPJL.java

package com.vandagroup.common.po;

import com.vandagroup.common.bo.ESSBean;
import com.vandagroup.common.bo.ESSObject;
import com.vandagroup.common.db.ESSDataStoreManager;
import com.vandagroup.common.exp.ESSConnectionGetTimeOutException;
import java.sql.*;
import java.util.Vector;

// Referenced classes of package com.vandagroup.common.po:
//            ESSPL, ESSPO

public class ESSPJL extends ESSPL
{

    ESSPO POs[];
    String outerJoinClause;
    public boolean distinct;

    public ESSPJL()
    {
        POs = null;
        outerJoinClause = "";
        distinct = true;
    }

    public void setOuterJoinClause(String newOuterJoinClause)
    {
        outerJoinClause = newOuterJoinClause;
    }

    public int getAvailableCount()
    {
        return recordCounter;
    }

    public void retrieve()
        throws SQLException, ESSConnectionGetTimeOutException
    {
        if(resultList != null)
            resultList.removeAllElements();
        int poNum = POs.length;
        StringBuffer SQL = new StringBuffer(300);
        SQL.append("SELECT ");
        if(distinct)
            SQL.append(" DISTINCT ");
        for(int i = 0; i < poNum; i++)
        {
            if(i != 0)
                SQL.append(" , ");
            SQL.append(POs[i].getColumnNames());
        }

        SQL.append(" FROM ");
        if(outerJoinClause.length() > 0)
        {
            SQL.append(outerJoinClause);
        } else
        {
            for(int i = 0; i < poNum; i++)
            {
                SQL.append(POs[i].getTableName());
                if(i != poNum - 1)
                    SQL.append(" , ");
            }

        }
        if(whereClause != null && whereClause.length() > 0)
        {
            SQL.append(" WHERE ");
            SQL.append(whereClause);
        }
        if(orderBy != null && orderBy.length() > 0)
        {
            SQL.append(" ORDER BY ");
            SQL.append(orderBy);
        }
        if(groupBy != null && groupBy.length() > 0)
        {
            SQL.append(" GROUP BY ");
            SQL.append(groupBy);
        }
        PreparedStatement stmt = null;
        Connection conn = null;
        ResultSet rs = null;
        try
        {
            conn = ESSObject.getDataStore().getConnection();
            stmt = conn.prepareStatement(SQL.toString());
            rs = stmt.executeQuery();
            fetchResults(rs);
        }
        catch(ESSConnectionGetTimeOutException ex)
        {
            throw ex;
        }
        catch(SQLException ex)
        {
            throw ex;
        }
        finally
        {
            rs.close();
            stmt.close();
            ESSObject.getDataStore().freeConnection(conn);
        }
        return;
    }

    public void fetchResults(ResultSet rs)
        throws SQLException
    {
        int poNum = POs.length;
        Class referenceClasses[] = new Class[poNum];
        for(int i = 0; i < poNum; i++)
            referenceClasses[i] = POs[i].getClass();

        int cursorPosition = 0;
        while(rs.next()) 
        {
            if(cursorPosition < startPosition)
            {
                cursorPosition++;
                continue;
            }
            if(resultSize > 0 && cursorPosition - startPosition == resultSize)
            {
                cursorPosition++;
                break;
            }
            Object objArray[] = new Object[poNum];
            ESSPO nextObject = null;
            for(int i = 0; i < poNum; i++)
            {
                try
                {
                    nextObject = (ESSPO)referenceClasses[i].newInstance();
                }
                catch(Exception exception) { }
                org.apache.struts.validator.ValidatorForm bean = nextObject.fetchResult(rs);
                objArray[i] = bean;
            }

            addToResultList(objArray);
            cursorPosition++;
        }
        while(rs.next()) 
            cursorPosition++;
        recordCounter = cursorPosition;
    }

    public void setPOs(ESSPO refObjs[])
    {
        POs = refObjs;
    }

    public ESSBean[][] getResultList2()
    {
        ESSBean beans[][] = new ESSBean[resultList.size()][POs.length];
        for(int index = 0; index < resultList.size(); index++)
        {
            ESSBean bean[] = (ESSBean[])resultList.elementAt(index);
            for(int col = 0; col < bean.length; col++)
                beans[index][col] = bean[col];

        }

        return beans;
    }

    public void addToResultList(Object row[])
    {
        resultList.addElement(((Object) (row)));
    }

    public void setDistinct(boolean newDistinct)
    {
        distinct = newDistinct;
    }
}
